<?php
class WorksAction extends Action{
	private $ws;
	private $work;
	private $thumbConfig;

	public function __construct()
	{
		parent::__construct();
		if($this->session->getAttr("uid") == null){
			$this->forward("Index");
		}
		$this->ws = new WorksetModel();
		$this->work = new WorkModel();
		$this->thumbConfig = array('width' => 180, 'height' => 180);
	}
	
	protected function init()
	{

	}
	public function display()
	{
		
	}
	public function showWork()
	{
		$wid = $this->get['wid'];
		$wc = new WorksCommentModel();

		$this->work->addCondition('work_id', '=', $wid, "");
		$wc->addCondition('wc_work_id', '=', $wid, "");

		$work = $this->work->findOne();
		$this->work->pageview++;
		$this->work->addCondition('work_id', '=', $work['work_id'], "");
		$this->work->update();
		$comments = $wc->find();

		$this->smarty->assign('work', $work);
		$this->smarty->assign('comments', $comments);
		$this->smarty->display("Works-showWork.tpl");
	}
	public function setIndex()
	{
		$sigs = array(
			'works_set_id' => array('name' => "作品集ID", 'required' => true, 'type' => 'int'),
			'works_set_index_id' => array('name' => '封面作品', 'required' => true, 'type' => 'int')
			);

		$this->checkVars($this->post, $sigs);

		$this->ws->create();

		$this->ws->addCondition("works_set_id", "=", $this->ws->works_set_id, "");
		$this->ws->update();

		$this->forward("Works", "showWorkSet");
	}
	/**
	 * 显示某一作者的所有作品
	 */
	public function showWorks()
	{
		$uid = $this->get['uid'] ? $this->get['uid'] : $this->session->getAttr('uid');
		$this->work->addCondition("author_id", "=", $uid, " order by `work_time` desc");

		$works = $this->work->find();

		$this->smarty->assign("works", $works);
		$this->smarty->display("Works-showWorks.tpl");
	}
	/**
	 * 显示某一作者的某一作品集
	 */
	public function showWorkSet()
	{
		$set_id = $this->get['set_id'];
		$uid = $this->get['uid'] ? $this->get['uid'] : $this->session->getAttr('uid');

		$this->ws->addCondition('works_set_id', '=', $set_id, "");

		$ws = $this->ws->findOne();

		$this->work->addCondition('author_id', '=', $uid, "AND");
		$this->work->addCondition('works_set_id', '=', $set_id, "");

		$works = $this->work->find();

		$this->smarty->assign("works", $works);
		$this->smarty->assign("ws", $ws);
		$this->smarty->display("Works-showWorks.tpl");
	}
	/**
	 * 显示某一作者的所有作品集
	 */
	public function showWorkSets()
	{
		$uid = $this->get['uid'] ? $this->get['uid'] : $this->session->getAttr('uid');
		$sql = 'SELECT ws.*, w.work_thumb_url from works_set as ws, works as w where ws.works_set_index_id = w.work_id';

		$sets = $this->ws->excuteSql($sql);

		$this->ws->addCondition('author_id' , '=', $uid, "");
		$worksets = $this->ws->find();

		$this->smarty->assign("worksets", $worksets);
		$this->smarty->display("Works-showWS.tpl");
	}
	/**
	 * 显示添加作品集的界面
	 */
	public function addWorkSet()
	{
		$this->smarty->display("Works-addWS.tpl");
	}
	/**
	 * 执行添加作品集的动作
	 */
	public function doAddWorkSet()
	{
		$signatures = array(
			'works_set_name' => array('name' => '作品集名称', 'required' => true, 'type' => 'string'),
			'works_set_description' => array('name' => '作品集简介', 'required' => true, 'type' => 'string'),
			'works_set_privacy' => array('name' => '隐私策略', 'required' => true, 'type' => 'int')
			);

		$this->checkVars($this->post, $signatures);

		$this->ws->create();
		$this->ws->author_id = $this->session->getAttr("uid");

		$msg = $this->ws->add() ? "添加成功!" : "添加失败!";

		$this->showMessage($msg);
	}
	/**
	 * 显示添加作品的界面
	 */
	public function addWorks()
	{
		$this->ws->addCondition('author_id', '=', $this->session->getAttr('uid'), "");
		$worksets = $this->ws->find();

		$this->smarty->assign("worksets", $worksets);
		$this->smarty->display("Works-addWork.tpl");
	}
	/**
	 * 执行添加作品的动作
	 */
	public function doAddWorks()
	{
		$signatures = array(
			'work_name' => array('name' => '作品名称', 'required' => true, 'type' => 'string'),
			'work_description' => array('name' => '作品描述', 'required' => true, 'type' => 'string'),
			'works_set_id' => array('name' => '所属作品集', 'required' => true, 'type' => 'int'),
			'privacy' => array('name' => '隐私策略', 'required' => true, 'type' => 'int')
			);
		$this->checkVars($this->post, $signatures);
/*
		$this->ws->addCondition('works_set_id', '=', $this->post['works_set_id'], "");

		$ws = $this->ws->findOne();

		if(!$ws)
		{
			$this->showMessage("对不起，该作品集不存在!");
		}
 */

		$this->work->create();

		if(!isset($this->files['work_file']))
		{
			$this->showMessage("请选择一个图片文件上传！");
		}

		// TODO: 检查文件类型和大小

		$user = $this->session->getAttr("user");
		$type = $this->files['work_file']['type'];
		$suffix = substr($type, strpos($type, "/")+1, strlen($type));
		$upload_path = UPLOAD_DIR . "/" . $user['email'] . "/". time(). "." .$suffix;
		$thumb_path = UPLOAD_DIR . "/" . $user['email'] .'/thumbs/'. time() . '.jpg';

		if(!move_uploaded_file($this->files['work_file']['tmp_name'], $upload_path))
		{
			$this->showMessage("对不起，文件上传失败！");
		}else{
			$url = "http://" . $_SERVER['SERVER_NAME'] . $this->getRelPath($upload_path);
			$thumb_url = "http://" . $_SERVER['SERVER_NAME'] . $this->getRelPath($thumb_path);

			if(!$this->genThumb($upload_path, $thumb_path))
			{
				$thumb_url = $url;
			}
			$this->work->work_image_url = $url;
			$this->work->work_thumb_url = $thumb_url;
			$this->work->author_id = $this->session->getAttr("uid");

			$msg = $this->work->add() ? "上传作品成功!" : "上传作品失败!";
			/*
			if($ws->works_set_index_id == 0)
			{
				$ws->works_set_index_id = mysql_insert_id();
			}
			$this->ws->addCondition('works_set_id', '=', $this->post['works_set_id'], "");
			$this->ws->update();
			 */

			$this->showMessage($msg);
		}
	}
	private function getRelPath($path)
	{
		$path = str_replace("\\", "/", $path);
		$root = str_replace("\\", "/", $_SERVER['DOCUMENT_ROOT']);
		$rel_path = substr($path, strlen($root), strlen($path));

		return $rel_path;
	}

	public function doView()
	{
		$this->showWorkSets();
	}


	public function test()
	{
		$file = 'D:/xampp/htdocs/jianzhusheng/uploads/dzy0451@gmail.com/1219367364.jpeg';
		$to_dir = 'D:/xampp/htdocs/jianzhusheng/uploads/dzy0451@gmail.com/thumbs';
		$this->genThumb($file, $to_dir);
	}

	public function genThumb($filepath, $to_path)
	{
		$img = null;
		$size_info = getimagesize($filepath);
		$type = $size_info[2];
		if($type == 2) // JPG
		{
			$img = @imagecreatefromjpeg($filepath);
		}
		else if($type == 3) // PNG
		{
			$img = @imagecreatefrompng($filepath);
		}
		else if($type == 1) // GIF
		{
			$img = @imagecreatefromgif($filepath);
		}
		else{
			return true;
		}

		if($img)
		{
			$width = $size_info[0];
			$height = $size_info[1];

			$scale = min((double)$this->thumbConfig['width'] / (double)$width, (double)$this->thumbConfig['height'] / (double)$height);

			if($scale < 1)
			{
				$new_width = floor($scale * $width);
				$new_height = floor($scale * $height);

				$thumb = imagecreatetruecolor($new_width, $new_height);

				imagecopyresized($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
				imagedestroy($img);
				$img = $thumb;
				if(!$img)
				{
					return false;
				}else
				{
					imagejpeg($img, $to_path);
					return true;
				}
			}else{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
};
?>
